package com.ruoyi.ruoyiqywechat.handle;

import com.alibaba.nacos.api.remote.response.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {

    // 简化版本 - 只返回状态码和简单消息
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseEntity<Map<String, Object>> handleUnauthorizedException(UnauthorizedException ex) {
        Map<String, Object> body = new HashMap<>();
        body.put("error", "Unauthorized");
        body.put("message", ex.getMessage());
        body.put("code", 401);
        body.put("timestamp", LocalDateTime.now());

        System.out.println("当前错误信息："+body);
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(body); // 必须添加body
    }
}
